/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package uk.co.droidinactu.common.file;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

/**
 * This class holds functions related to file manipulation.
 * 
 * @author andy
 */
public class FileUtilities {

	/**
	 * copy one file to a new file.
	 * 
	 * @param in
	 *            the file to make the copy of.
	 * @param out
	 *            the destination for the copy of the file.
	 * @throws Exception
	 */
	public static void copyFile(final File in, final File out) throws Exception {
		final FileInputStream fis = new FileInputStream(in);
		final FileOutputStream fos = new FileOutputStream(out);
		try {
			final byte[] buf = new byte[1024];
			int i = 0;
			while ((i = fis.read(buf)) != -1) {
				fos.write(buf, 0, i);
			}
		} catch (final Exception e) {
			throw e;
		} finally {
			fis.close();
			fos.close();
		}
	}

	public static String getApplicationRootDir(final String appName) {
		final File extDir = Environment.getExternalStorageDirectory();
		return extDir.getPath() + File.separator + appName + File.separator;
	}

	public static boolean StoreByteImage(final Context mContext, final byte[] imageData,
			final int quality, final String imgDirectory, final String filename) {

		// File sdImageMainDirectory = new File(imgDirectory);
		FileOutputStream fileOutputStream = null;
		try {

			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 5;

			final Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,
					options);

			final String newFilename = imgDirectory + File.separator + filename + ".jpg";
			fileOutputStream = new FileOutputStream(newFilename);

			final BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

			myImage.compress(CompressFormat.JPEG, quality, bos);

			bos.flush();
			fileOutputStream.flush();

			bos.close();
			fileOutputStream.close();

		} catch (final FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

	public boolean isSdCardPresent() {
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			Log.i("FileUtilities", "SD Card is installed and mounted");
			return true;
		}
		return false;
	}

}
